﻿package
{
	import flash.display.Sprite;
	import nl.joeyvandijk.Data;
	import nl.joeyvandijk.Logo;
	import nl.joeyvandijk.Pics;
	import nl.joeyvandijk.Show;
	import nl.joeyvandijk.Preloader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Demo extends Sprite
	{
		private var data:Data;
		private var pics:Pics;
		private var preloader:Preloader;
		private var show:Show;
		private var logo:Logo;
		public static var STAGE_HEIGHT:uint = 200;
		
		public function Demo()
		{
			
			
			preloader = new Preloader();
			preloader.x = (stage.stageWidth/2) - 100;
			preloader.y = 98;
			addChild(preloader);
			
			show = new Show();
			addChild(show);
			
			pics = new Pics();
			pics.addEventListener(Event.COMPLETE, picsLoaded);
			pics.addEventListener(Event.CHANGE, picsLoading);
			
			data = new Data();
			data.addEventListener(Event.COMPLETE, dataLoaded);
			data.load('/flash/data.xml');
			
			logo = new Logo();
			addChild(logo);
			
			mouseChildren = true;
			
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0xffffff,0);
			spr.graphics.drawRect(0,0,890,200);
			spr.graphics.endFill();
			spr.buttonMode = true;
			spr.useHandCursor = true;
			spr.mouseChildren = false;
			spr.addEventListener(MouseEvent.ROLL_OVER, mouse);
			spr.addEventListener(MouseEvent.ROLL_OUT, mouse);
			spr.addEventListener(MouseEvent.CLICK, mouse);
			addChild(spr);
		}
		
		private function mouse(e:MouseEvent):void
		{
			trace('mouse:'+e);
			switch(e.type)
			{
				case MouseEvent.CLICK:
					show.click();
					break;
			}
		}
		
		private function dataLoaded(e:Event):void
		{
			//load assets, start preloader
			pics.load(data.items);
		}
		
		private function picsLoaded(e:Event):void
		{
			//start animation
			preloader.hide();
			
			trace('start');
			show.start(data.items, pics.items, logo);
		}
		
		private function picsLoading(e:Event):void
		{
			trace('LOADING: '+pics.progress+'%');
			preloader.change(pics.progress);
		}
	}	
}